﻿using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using System.Windows;
using System.Windows.Media.Animation;
using HMI.View;

namespace HMI.Model
{
    public class MainWindowViewModel : BindableBase
    {
        public MainWindowViewModel() {
            //窗体关闭
            Loaded = new DelegateCommand<MainWindow>(new Action<MainWindow>(LoadedEvent));
        }
        public DelegateCommand<MainWindow> Loaded { get; set; }
        private void LoadedEvent(MainWindow mainWindow)
        {
            mainWindow.header.TitleTxt = "四川王力智能产线";
            FadeFrm(mainWindow);
            InitializeFrm(mainWindow);
            mainWindow.header.MinFrm += new RoutedEventHandler((object sender, RoutedEventArgs e) =>
            {
                mainWindow.WindowState = WindowState.Minimized;
            });
            mainWindow.header.MaxFrm += new RoutedEventHandler((object sender, RoutedEventArgs e) =>
            {
                if (mainWindow.header.max)
                {
                    mainWindow.WindowState = WindowState.Maximized;
                }
                else
                {
                    mainWindow.WindowState = WindowState.Normal;
                }
            });
            mainWindow.header.CloseFrm += new RoutedEventHandler((object sender, RoutedEventArgs e) =>
            {
                Application.Current.Shutdown();
            });
            mainWindow.header.MouseLeftButtonDown += (object sender, MouseButtonEventArgs e) =>
            {
                switch (e.ClickCount)
                {
                    case 1:
                        mainWindow.DragMove();
                        break;
                }
            };
        }

        private void FadeFrm(MainWindow mainWindow)
        {
            mainWindow.header.max = true;
            mainWindow.WindowState = WindowState.Maximized;
            mainWindow.Opacity = 0;
            DoubleAnimation backBoardAnimation = new DoubleAnimation();
            backBoardAnimation.BeginTime = TimeSpan.FromSeconds(0.1);
            backBoardAnimation.FillBehavior = FillBehavior.HoldEnd;
            backBoardAnimation.Duration = new Duration(TimeSpan.FromSeconds(1));
            backBoardAnimation.From = 0;
            backBoardAnimation.To = 1;
            backBoardAnimation.BeginTime = TimeSpan.FromSeconds(0.01);
            mainWindow.BeginAnimation(Window.OpacityProperty, backBoardAnimation);
        }
        void InitializeFrm(MainWindow mainWindow)
        {
            HomeForm home =new HomeForm();
            //home.FillData();
            mainWindow.containx.FillControl(1, home);
            OrderFrm orderFrm = new OrderFrm();
            mainWindow.containx.FillControl(2, orderFrm);
            LogFrm logfrm = new LogFrm();
            mainWindow.containx.FillControl(5, logfrm);
            DeviceFrm deviceFrm = new DeviceFrm();
            mainWindow.containx.FillControl(4, deviceFrm);
            SetupFrm setupFrm = new SetupFrm();
            mainWindow.containx.FillControl(3, setupFrm);
            UserFrm userFrm = new UserFrm();
            //userFrm.FillData();
            //userFrm.LoginOut = new System.Action(() =>
            //{
            //    //apph.UserLogin = UserType.Commen;
            //    //SysModels.Lines.CurrentUser = null;
            //    //DbUser.ClearUserCurrent();
            //    userFrm.FillData();
            //});
            mainWindow.containx.FillControl(6, userFrm);
        }
    }
}
